package pATT.profilingAlgorithm.PersonalSearcher;


import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;



public class StemDictionary extends Hashtable {

		/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

		public StemDictionary(){
				super();
		}

		@SuppressWarnings("unchecked")
		public void add(String word, String stem){
				if (get(stem) != null){
						Hashtable words = (Hashtable)get(stem);
						if (words.get(word) != null){
								Integer i = (Integer)words.get(word);
								i = new Integer(i.intValue()+1);
								words.put(word,i);
						}
						else{
								Integer f = new Integer(1);
								words.put(word,f);
						}
						put(stem,words);
				}
				else{
						Hashtable words = new Hashtable();
						Integer f = new Integer(1);
						words.put(word, f);
						put(stem,words);
				}					
		}
		
		public void merge(Hashtable stems){
				if ((stems!=null)&&(stems.size()>0)){
						for (Enumeration enum2 =stems.keys(); enum2.hasMoreElements() ;) {
								String stem=(String)enum2.nextElement();
								Hashtable h=(Hashtable)stems.get(stem);
								for (Enumeration e =h.keys(); e.hasMoreElements() ;) {
										String word=(String)e.nextElement();
										add(word,stem);
								}
						}
				}
		}

		@SuppressWarnings("unchecked")
		public String getBestWord(String stem){
				Hashtable info = (Hashtable)get(stem);
				if ((info!=null)&&(!info.isEmpty())){
						Vector stems = new Vector();
						for (Enumeration enum2 = info.keys(); enum2.hasMoreElements() ;) {
								String w=(String)enum2.nextElement();
								Integer i=(Integer)info.get(w);
								stems.add(new ObjectValuePair(w, i.doubleValue()));
						}
						QuickSort.sort(stems);
						ObjectValuePair prim = (ObjectValuePair)stems.get(0);
						return (String)prim.getObject();
				}
				return "";
		}

		public String getWords(String stem){
				Hashtable info = (Hashtable)get(stem);
				String words = "";
				for (Enumeration enum2 =info.keys(); enum2.hasMoreElements() ;) {
					String word=(String)enum2.nextElement();
					words = words + word + ", ";
				}
				return words.substring(0,words.lastIndexOf(","));
		}

		public TermVector getNoStemWords(String stem){
				Hashtable info = (Hashtable)get(stem);
				TermVector words = new TermVector();
				for (Enumeration enum2 =info.keys(); enum2.hasMoreElements() ;) {
					String word=(String)enum2.nextElement();
					words.put(word,1);
				}
				return words;
		}
}
